package binary;

public class Demo10 {
    public static void main(String[] args) {
        int n = 0x6a7b9cef;
        System.out.println(Integer.toBinaryString(n));
        byte[] bytes = toBytes(n);
        System.out.println(Integer.toBinaryString(bytes[0] & 0xff));
        System.out.println(Integer.toBinaryString(bytes[1] & 0xff));
        System.out.println(Integer.toBinaryString(bytes[2] & 0xff));
        System.out.println(Integer.toBinaryString(bytes[3] & 0xff));
        int m = toInt(bytes);
        System.out.println(Integer.toBinaryString(m));
    }
    public static byte[] toBytes(int n){
        int b1 = (n >>> 24) & 0xff;
        int b2 = (n >>> 16) & 0xff;
        int b3 = (n >>> 8) & 0xff;
        int b4 = (n >>> 0) & 0xff;
        return new byte[]{(byte)b1, (byte)b2, (byte)b3, (byte)b4};
    }
    public static int toInt(byte[] bytes){
        int b1 = bytes[0] & 0xff; //  10111011
        int b2 = bytes[1] & 0xff;
        int b3 = bytes[2] & 0xff;
        int b4 = bytes[3] & 0xff;
        return (b1<<24) | (b2<<16) | (b3<<8) | (b4<<0);
    }
}
